package morenoapp.components.workplace;

import com.jgoodies.forms.builder.PanelBuilder;
import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import morenoapp.MorenoApp;
import morenoapp.components.conclusion.ConclusionMainJPanel;
import morenoapp.components.conclusion.ConclusionMainPanel;
import morenoapp.components.config2.Configuration2JPanel;
import morenoapp.components.input.InputMainJPanel;
import morenoapp.components.thinkgraphv2.ThinkgraphV2JPanel;
import morenoapp.components.thinktable.ThinktableMainJPanel;
import morenoapp.components.welcome.WelcomeMainJPanel;
import morenoapp.model.enums.MorenoModelStage;
import morenoapp.util.I18N;

/**
 *
 * @author dmitry.mamonov
 */
public class Workplace implements WorkplaceNavigator{
    WelcomeMainJPanel welcomeView = new WelcomeMainJPanel();
    JPanel researchView = new Configuration2JPanel();
    InputMainJPanel inputView = new InputMainJPanel();
    ThinktableMainJPanel sociomatrixView = new ThinktableMainJPanel();
    ThinkgraphV2JPanel sociogramView = new ThinkgraphV2JPanel();
    //ConclusionMainJPanel conclusionView = new ConclusionMainJPanel();
    ConclusionMainPanel conclusionView = new ConclusionMainPanel();
    Map<JComponent,JPanel> substitutionMap = new HashMap<JComponent, JPanel>();

    TabbedWorkplace tabbed;
    WizardWorkplace wizard;
    WorkplaceNavigator navigator;
    WorkplaceView currentView = WorkplaceView.WelcomeView;
    Set<WorkspaceNavigatorListener> navigatorListeners = new HashSet<WorkspaceNavigatorListener>();

    PropertyChangeListener modelStageListener = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            if (evt.getPropertyName().equals("stage")){
                boolean newModel = MorenoModelStage.NEW_MODEL.isSubstageOf((MorenoModelStage)evt.getNewValue());

                researchView.setEnabled(newModel);
                inputView.setEnabled(newModel);
                sociomatrixView.setEnabled(newModel);
                sociogramView.setEnabled(newModel);
                conclusionView.setEnabled(newModel);

                //update current components state
                if (navigator instanceof TabbedWorkplace){
                    ((TabbedWorkplace)navigator).init(Workplace.this);
                } else {
                    ((WizardWorkplace)navigator).init(Workplace.this);
                }
            }
        }
    };


    public Workplace() {
        super();
        tabbed = new TabbedWorkplace(this);
        wizard = new WizardWorkplace(this);
        navigator = wizard;

        MorenoApp.getModel().addPropertyChangeListener(modelStageListener);
    }



    public JComponent getView(WorkplaceView view) {
        JPanel result;
        switch (view) {
            case WelcomeView: result = welcomeView; break;
            case ResearchView: result = researchView; break;
            case InputView: result = inputView; break;
            case SociomatrixView: result = sociomatrixView; break;
            case SociogramView: result = sociogramView; break;
            case ConclutionView: result = conclusionView; break;
            default: throw new UnsupportedOperationException("Unknown view type: "+view);
        }
        if (result.isEnabled()==true){
            return result;
        } else {
            if (substitutionMap.containsKey(result)==false){
                FormLayout layout = new FormLayout("20px:grow","20px:grow");
                PanelBuilder builder = new PanelBuilder(layout);
                CellConstraints cc = new CellConstraints();
                JLabel label = new JLabel(I18N.get("message.please_create_new_reasearch_at_beginning"));
                builder.add(label, cc.rc(1,1,"center,center"));
                substitutionMap.put(result, builder.getPanel());
            }
            return substitutionMap.get(result);
        }
    }

    private void cleanup(){
        tabbed.cleanup();
        wizard.cleanup();
    }

    /**
     * Otherwice it's tabbed mode.
     * @return
     */
    public boolean isWizardMode(){
        return navigator==wizard;
    }

    public WizardWorkplace getWizardWorkplace(){
        cleanup();
        wizard.init(this);
        navigator = wizard;
        return wizard;
    }

    public TabbedWorkplace getTabbedWorkplace(){
        cleanup();
        tabbed.init(this);
        navigator = tabbed;
        return tabbed;
    }

    void setCurrentView(WorkplaceView view){
        currentView = view;
        for(WorkspaceNavigatorListener lst: navigatorListeners){
            lst.onSelectedView(view);
        }
    }

    @Override
    public WorkplaceView getCurrentView() {
        return currentView;
    }

    @Override
    public JComponent getCurrentViewComponent() {
        return getView(getCurrentView());
    }

    @Override
    public boolean activateNextView() {
        WorkplaceView next = getCurrentView().next();
        if (next!=null){
            navigator.activateView(next);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean activatePrevView() {
        WorkplaceView prev = getCurrentView().prev();
        if (prev!=null){
            navigator.activateView(prev);
            return true;
        } else {
            return false;
        }
    }

    @Override
    public void activateView(WorkplaceView view) {
        navigator.activateView(view);
    }

    @Override
    public void addNavigatorListener(WorkspaceNavigatorListener lst) {
        navigatorListeners.add(lst);
    }

    @Override
    public void removeNavigatoListener(WorkspaceNavigatorListener lst) {
        navigatorListeners.remove(lst);
    }
}
